﻿using System;

public class Mapper032 : MapperDefault
{
    internal int patch;
    internal int[] regs = new int[1];

    public override void init(NES nes)
    {
        base.init(nes);
    }

    public override void loadROM(ROM rom)
    {
        int num = rom.RomBankCount * 2;
        this.load8kRomBank(0, 0x8000);
        this.load8kRomBank(1, 0xa000);
        this.load8kRomBank(num - 2, 0xc000);
        this.load8kRomBank(num - 1, 0xe000);
        this.loadCHRROM();
        base.nes.Cpu.requestIrq(2);
    }

    public override void reset()
    {
        if (this.patch == 1)
        {
            base.nes.Ppu.Mirroring = 3;
        }
        for (int i = 0; i < this.regs.Length; i++)
        {
            this.regs[i] = 0;
        }
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            switch ((address & 0xf000))
            {
                case 0x8000:
                    if ((this.regs[0] & 2) != 0)
                    {
                        this.load8kRomBank(value_Renamed, 0xc000);
                    }
                    else
                    {
                        this.load8kRomBank(value_Renamed, 0x8000);
                    }
                    break;

                case 0x9000:
                    if ((value_Renamed & 1) != 0)
                    {
                        base.nes.Ppu.Mirroring = 1;
                    }
                    else
                    {
                        base.nes.Ppu.Mirroring = 0;
                    }
                    this.regs[0] = value_Renamed;
                    break;

                case 0xa000:
                    this.load8kRomBank(value_Renamed, 0xa000);
                    break;
            }
            switch ((address & 0xf007))
            {
                case 0xb000:
                    this.load1kVromBank(value_Renamed, 0);
                    return;

                case 0xb001:
                    this.load1kVromBank(value_Renamed, 0x400);
                    return;

                case 0xb002:
                    this.load1kVromBank(value_Renamed, 0x800);
                    return;

                case 0xb003:
                    this.load1kVromBank(value_Renamed, 0xc00);
                    return;

                case 0xb004:
                    this.load1kVromBank(value_Renamed, 0x1000);
                    return;

                case 0xb005:
                    this.load1kVromBank(value_Renamed, 0x1400);
                    return;

                case 0xb006:
                    if (this.patch == 1)
                    {
                        int num1 = value_Renamed & 0x40;
                    }
                    this.load1kVromBank(value_Renamed, 0x1800);
                    return;

                case 0xb007:
                    if ((this.patch == 1) && ((value_Renamed & 0x40) != 0))
                    {
                        base.nes.Ppu.Mirroring = 3;
                    }
                    this.load1kVromBank(value_Renamed, 0x1c00);
                    return;
            }
        }
    }

    public override long CRC
    {
        set
        {
            this.patch = 0;
            if (value == 0x243a8735L)
            {
                this.patch = 1;
            }
        }
    }
}

